package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.google.gson.JsonObject;
import com.jiayen.cache.FileCacheManager;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.adapter.HelpAdapter;
import com.jp.knowledge.my.adapter.HelpCateListAdapter;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.model.HelpCateModel;
import com.jp.knowledge.my.model.HelpModel;
import com.jp.knowledge.util.ResultSubscriber;
import com.jp.knowledge.view.JpDiver;

import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import rx.Observable;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 528787067@qq.com on 2017/6/21.
 */

public class HelpCateListActivity extends SlidingActivity {

	@ViewInject(R.id.help_content_rv)
	private RecyclerView helpRv;
	@ViewInject(R.id.bottom_bar)
	private LinearLayout bottom;

	private List<HelpCateModel> helpCateModels;
	private HelpCateListAdapter helpAdapter;
	private String cachePath;
	private FileCacheManager cacheManager;
	private String id;

	@Override
	protected int getResourceId() {
		return R.layout.activity_help;
	}

	@Override
	protected void init() {
		id = getIntent().getStringExtra("cateId");
		String title = getIntent().getStringExtra("title");
		topName.setText(title == null ? "帮助" : title);
		rightIcon.setVisibility(View.GONE);
		leftIcon.setImageResource(R.mipmap.arrow_white_left);
		leftIcon.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});

		helpCateModels = new ArrayList<>();
		helpAdapter = new HelpCateListAdapter(mContext, helpCateModels);
		bottom.setVisibility(View.GONE);
		helpRv.setHasFixedSize(true);
		helpRv.addItemDecoration(new JpDiver(mContext));
		helpRv.setLayoutManager(new LinearLayoutManager(mContext));
		helpRv.setAdapter(helpAdapter);
		helpAdapter.addItemClickListener(new RecycleVAdapter.OnItemClickListener() {
			@Override
			public void itemSelect(int position) {
				startActivity(new Intent(mContext, HelpCatePageActivity.class)
						.putExtra("title", helpCateModels.get(position).getTitle())
						.putExtra("pageId", helpCateModels.get(position).getPageId()));
			}
		});

		cachePath = mContext.getCacheDir() + "/helpCateList" + id + ".dat";
		cacheManager = new FileCacheManager();
		readCache();
	}

	private void getData(){
		JsonObject param = new JsonObject();
		param.addProperty("id", id);
		HttpHelper.getInstance(mContext).helpCateList(param, 0x01, new ResultSubscriber.OnResultListener() {
			@Override
			public void onStart(int requestType) {}
			@Override
			public void onCompleted(int requestType) {}
			@Override
			public void onError(int requestType) {
				onCompleted(requestType);
			}
			@Override
			public void onNext(IModel data, int requestType) {
				if(data.getErrcode() != 0){
					ToasUtil.toast(mContext, data.getMessage());
					onError(requestType);
				} else{
					helpCateModels.clear();
					List<HelpCateModel> helpCateModelList = data.getList(HelpCateModel.class);
					if(helpCateModelList != null && helpCateModelList.size() > 0)
						helpCateModels.addAll(helpCateModelList);
					helpAdapter.updateAdapter(helpCateModels);
					saveCache(cachePath, helpCateModels);
				}
			}
		});
	}
	private void readCache(){
		Observable.create(new Observable.OnSubscribe<List<HelpCateModel>>() {
			@Override
			public void call(Subscriber<? super List<HelpCateModel>> subscriber) {
				List<HelpCateModel> appData = (List<HelpCateModel>) cacheManager.readObject(cachePath);
				subscriber.onNext(appData);
			}
		}).subscribeOn(Schedulers.io())
		.observeOn(AndroidSchedulers.mainThread())
		.subscribe(new Subscriber<List<HelpCateModel>>() {
			@Override
			public void onCompleted() {
				getData();
			}
			@Override
			public void onError(Throwable e) {
				onCompleted();
			}
			@Override
			public void onNext(List<HelpCateModel> data) {
				if(data != null && data.size() > 0){
					helpCateModels.clear();
					helpCateModels.addAll(data);
					helpAdapter.updateAdapter(helpCateModels);
				}
				onCompleted();
			}
		});
	}

	private void saveCache(final String cachePath, final Object data){
		new Thread(new Runnable() {
			@Override
			public void run() {
				cacheManager.wirteObject(data, cachePath);
			}
		}).start();
	}
}
